<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Stages Model
*
* Author: John Schop, john.schop@gmail.com
*
*/

class Stages_model extends CI_Model
{
	

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	

	/**
	 * Get all Stages
	 *
	 * @access public
	 * @return object
	 */
	function get_all()
	{
		$this->db->order_by('stages.stage_id', 'asc');
		return $this->db->get('stages')->result();
	}

    /**
     * Get stage by id
     *
     * @access public
     * @param string $stage_id
     * @return object stage object
     */
    function get_by_id($stage_id)
    {
        return $this->db->get_where('stages', array('stage_id' => $stage_id))->row();
    }

    function get_first()
    {
        $this->db->order_by('stage_date', 'asc');
        $this->db->limit(1);
        return $this->db->get('stages')->row();
    }

    function get_last_calculated()
    {
      $this->db->order_by('stage_date', 'desc');
      $this->db->where('calculated = 1');
      $this->db->limit(1);
      $stage = $this->db->get('stages')->row();
      if(is_object($stage))
      {
        $this->load->model('riders_model');
        $stage_result_array = explode(',',$stage->stage_result);
        $stage_yellow_array = explode(',',$stage->stage_yellow);
        $stage_green_array = explode(',',$stage->stage_green);
        $stage_mountain_array = explode(',',$stage->stage_mountain);
        foreach($stage_result_array as $rider_id)
        {
          $stage->result_riders[] = $this->riders_model->get_by_id($rider_id);
        }

        foreach($stage_yellow_array as $rider_id)
        {
          $stage->yellow_riders[] = $this->riders_model->get_by_id($rider_id);
        }

        foreach($stage_green_array as $rider_id)
        {
          $stage->green_riders[] = $this->riders_model->get_by_id($rider_id);
        }

        foreach($stage_mountain_array as $rider_id)
        {
          $stage->mountain_riders[] = $this->riders_model->get_by_id($rider_id);
        }
        return $stage;
      }
      else
      {
        return false;
      }
    }

    /**
     * Update stage details
     *
     * @access public
     * @param int $stage_id
     * @param array $attributes
     * @return void
     */
    function update($stage_id, $attributes = array())
    {

        // Update
        if ($this->get_by_id($stage_id))
        {
            $this->db->where('stage_id', $stage_id);
            $this->db->update('stages', $attributes);
        }
        // Insert
        else
        {
            $this->db->insert('stages', $attributes);
        }

    }


		/**
		 * Delete stage
		 *
		 * @access public
		 * @param int $stage_id
		 * @return void
		 */
    function delete($stage_id)
    {
    	$this->db->delete('stages', array('stage_id' => $stage_id)); 
    }

    function delete_all_results()
    {
      $stages = $this->get_all();
      $attributes = array('stage_result' => NULL,
                         'stage_yellow' => NULL,
                         'stage_green' => NULL,
                         'stage_mountain' => NULL,
                         'calculated' => 0);
      foreach($stages as $stage)
      {
        $this->update($stage->stage_id, $attributes);
      }

      $this->load->model('points_model');
      $this->points_model->delete_all();
      $this->load->model('riders_model');
      $this->riders_model->delete_all_results();
    }

}
